package cn.itsource.pethome.product.controller;

import cn.itsource.pethome.basic.util.PageInfo;
import cn.itsource.pethome.basic.util.Result;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.DeparmentQuery;
import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.domain.ProductDetail;
import cn.itsource.pethome.product.query.ProductQuery;
import cn.itsource.pethome.product.service.IProductDetailService;
import cn.itsource.pethome.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
@Api(tags = "服务产品接口",description = "服务产品接口详细描述")//swagger给生成的API做中文注释
public class ProductController {
    @Autowired
    private IProductDetailService productDetailService;

    @Autowired
    private IProductService productService;

    @PatchMapping//  相当于  @RequestMapping(value = "/department",method = RequestMethod.PATCH)
    public PageInfo<Product> findPageByQuery(@RequestBody ProductQuery productQuery) {
        return productService.findPageByQuery(productQuery);
    }

    /**
     * 新增和修改都走此方法
     *
     * @param product 如果id为空，就新增，如果id不为空就是修改
     * @return
     */
    @PostMapping// 相当于    @RequestMapping(value = "/product",method = RequestMethod.POST)
    @ApiOperation(value = "服务产品添加或修改", notes = "如果有id就是修改否则就是添加")//给对应交互风格做注解
    public Result save(@RequestBody Product product) {
        try {
            productService.save(product);
            return new Result();

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }

    }

    /**
     * 根据id删除数据
     *
     * @param
     * @return
     */
    @DeleteMapping("/{id}")//相当于@RequestMapping(value = "/product/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            productService.delete(id);
            return new Result("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }

    }

    /**
     * 批量删除
     *
     * @param products
     * @return
     */
    @PatchMapping("/batchRemove")//相当于@RequestMapping(value = "/product/{id}",method = RequestMethod.DELETE)
    public Result batchRemove(@RequestBody List<Product> products) {
        Map<String, Object> map = new HashMap<>();
        try {
            productService.batchRemove(products);
            return new Result("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }

    }

    /**
     * 批量上架服务
     *
     * @param products
     * @return
     */
    @PatchMapping("/batchOnSale")//相当于@RequestMapping(value = "/product/{id}",method = RequestMethod.DELETE)
    public Result batchOnSale(@RequestBody List<Product> products) {
        Map<String, Object> map = new HashMap<>();
        try {
            productService.batchOnSale(products);
            return new Result("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }

    }

    /**
     * 批量下架服务
     *
     * @param products
     * @return
     */
    @PatchMapping("/batchOffSale")//相当于@RequestMapping(value = "/product/{id}",method = RequestMethod.DELETE)
    public Result batchOffSale(@RequestBody List<Product> products) {
        Map<String, Object> map = new HashMap<>();
        try {
            productService.batchOffSale(products);
            return new Result("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }

    }



    //=============================================前台用户服务页面=====================================================//

    /**
     * 所有用户界面的服务列表
     * @param productQuery
     * @return
     */
    @PatchMapping("/findHomePageByQuery")//  相当于  @RequestMapping(value = "/department",method = RequestMethod.PATCH)
    public PageInfo<Product> findHomePageByQuery(@RequestBody ProductQuery productQuery) {
        return productService.findPageByQuery(productQuery);
    }



    /**
     * 根据id查询服务对象
     * @param id
     * @return  服务对象和服务明细数据
     */
    @GetMapping("/{id}")
    public Product findHomeById(@PathVariable("id") Long id){
        return productService.findById(id);
    }

}



